package com.alex.com.alex.livertmppushsdk.demo;

import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;

import com.kikt.commontools.SPInstance;
import com.kikt.commontools.util.ToastUtils;

public class StartActivity extends Activity implements View.OnClickListener {
    public static final String RTMPURL_MESSAGE = "com.alex.com.alex.livertmppushsdk.demo.rtmpurl";
    private static final String SP_PUSH_URL = "push_url";

    private Button _startRtmpPushButton = null;
    private EditText _rtmpUrlEditText = null;

    private View.OnClickListener _startRtmpPushOnClickedEvent = new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent i = new Intent(StartActivity.this, MainActivity.class);
            String rtmpUrl = _rtmpUrlEditText.getText().toString();

            i.putExtra(StartActivity.RTMPURL_MESSAGE, rtmpUrl);
            StartActivity.this.startActivity(i);
        }
    };
    protected View _saveUrlButton;
    protected SPInstance spInstance;

    private void initUI() {
        _rtmpUrlEditText = (EditText) findViewById(R.id.rtmpUrleditText);
        _startRtmpPushButton = (Button) findViewById(R.id.startRtmpButton);
        _saveUrlButton = findViewById(R.id.bt_save_url);

        //        _rtmpUrlEditText.setText("rtmp://ossrs.net/live/12345678");
        _rtmpUrlEditText.setText("rtmp://114.215.158.172/myapp/test");
        //        _rtmpUrlEditText.setText("rtmp://192.168.1.105/live/stream");

        _startRtmpPushButton.setOnClickListener(_startRtmpPushOnClickedEvent);
        _saveUrlButton.setOnClickListener(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        initUI();

        initData();
    }

    private void initData() {
        spInstance = SPInstance.getInstance(this);

        String url = spInstance.getString(SP_PUSH_URL, "");
        if (!TextUtils.isEmpty(url)) {
            _rtmpUrlEditText.setText(url);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_save_url:
                spInstance.putString(SP_PUSH_URL, _rtmpUrlEditText.getText().toString());
                ToastUtils.toast("保存成功");
                break;
        }
    }
}
